package view;

import view.utils.ExtensionFilter;
import controller.Controller;
import java.awt.event.KeyEvent;
import jgf.IGraph;

/**
 * 
 * @author Marcos Garcia Ortega <a href="mailto:thiamath@gmail.com">&lt;thiamath@gmail.com&gt;</a>
 */
public class MainFrame extends javax.swing.JFrame {

	/** Creates new form MainFrame */
	public MainFrame() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        createGraphDialog = new javax.swing.JDialog();
        createGraphDialog_nameLabel = new javax.swing.JLabel();
        createGraphDialog_nameTextField = new javax.swing.JTextField();
        createGraphDialog_graphRB = new javax.swing.JRadioButton();
        createGraphDialog_digraphRB = new javax.swing.JRadioButton();
        createGraphDialog_createB = new javax.swing.JButton();
        createGraphDialog_cancelB = new javax.swing.JButton();
        createGraphDialog_pseudoCB = new javax.swing.JCheckBox();
        createGraphDialog_MultipleCB = new javax.swing.JCheckBox();
        createGraphDialog_graphKindsGroup = new javax.swing.ButtonGroup();
        openDialog = new javax.swing.JFileChooser();
        desktop = new javax.swing.JDesktopPane();
        mainMenuBar = new javax.swing.JMenuBar();
        fileM = new javax.swing.JMenu();
        file_newGraphMI = new javax.swing.JMenuItem();
        file_openMI = new javax.swing.JMenuItem();
        file_separator = new javax.swing.JSeparator();
        file_exitMI = new javax.swing.JMenuItem();
        editM = new javax.swing.JMenu();

        createGraphDialog.setTitle("New Graph Dialog");
        createGraphDialog.setAlwaysOnTop(true);
        createGraphDialog.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        createGraphDialog.setModal(true);
        createGraphDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

        createGraphDialog_nameLabel.setText("Name of the graph:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_nameLabel, gridBagConstraints);

        createGraphDialog_nameTextField.setMinimumSize(new java.awt.Dimension(6, 40));
        createGraphDialog_nameTextField.setPreferredSize(new java.awt.Dimension(100, 20));
        createGraphDialog_nameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                createGraphDialog_nameTextFieldKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_nameTextField, gridBagConstraints);

        createGraphDialog_graphKindsGroup.add(createGraphDialog_graphRB);
        createGraphDialog_graphRB.setSelected(true);
        createGraphDialog_graphRB.setText("Graph");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_graphRB, gridBagConstraints);

        createGraphDialog_graphKindsGroup.add(createGraphDialog_digraphRB);
        createGraphDialog_digraphRB.setText("Digraph");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_digraphRB, gridBagConstraints);

        createGraphDialog_createB.setText("Create");
        createGraphDialog_createB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createGraphDialog_createBActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(20, 10, 10, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_createB, gridBagConstraints);

        createGraphDialog_cancelB.setText("Cancel");
        createGraphDialog_cancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createGraphDialog_cancelBActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(20, 10, 10, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_cancelB, gridBagConstraints);

        createGraphDialog_pseudoCB.setText("Pseudo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_pseudoCB, gridBagConstraints);

        createGraphDialog_MultipleCB.setText("Multiple");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 10);
        createGraphDialog.getContentPane().add(createGraphDialog_MultipleCB, gridBagConstraints);

        createGraphDialog.pack();

        openDialog.setCurrentDirectory(new java.io.File("."));
        openDialog.addChoosableFileFilter(new ExtensionFilter(
            "JGS graph", "gra"));
    openDialog.addChoosableFileFilter(new ExtensionFilter(
        "JGS digraph", "dig"));

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Java Graph Solver");
getContentPane().add(desktop, java.awt.BorderLayout.CENTER);

fileM.setMnemonic('F');
fileM.setText("File");

file_newGraphMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
file_newGraphMI.setText("New Graph");
file_newGraphMI.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        file_newGraphMIActionPerformed(evt);
    }
    });
    fileM.add(file_newGraphMI);

    file_openMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
    file_openMI.setText("Open");
    file_openMI.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            file_openMIActionPerformed(evt);
        }
    });
    fileM.add(file_openMI);
    fileM.add(file_separator);

    file_exitMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
    file_exitMI.setText("Exit");
    fileM.add(file_exitMI);

    mainMenuBar.add(fileM);

    editM.setMnemonic('E');
    editM.setText("Edit");
    mainMenuBar.add(editM);

    setJMenuBar(mainMenuBar);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-810)/2, (screenSize.height-629)/2, 810, 629);
    }// </editor-fold>//GEN-END:initComponents

	private void file_newGraphMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_file_newGraphMIActionPerformed

		createGraphDialog.setVisible(true);

}//GEN-LAST:event_file_newGraphMIActionPerformed

	private void createGraphDialog_createBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createGraphDialog_createBActionPerformed

		GraphIF graphIF = new GraphIF();
		this.desktop.add(graphIF);

		int type = 0;
		//OR clauses
		if (this.createGraphDialog_graphRB.isSelected()) {
			type += IGraph.GRAPH;
		} else if (this.createGraphDialog_digraphRB.isSelected()) {
			type += IGraph.DIGRAPH;
		}
		//AND clauses
		if (this.createGraphDialog_MultipleCB.isSelected()) {
			type += IGraph.MULTI;
		}
		if (this.createGraphDialog_pseudoCB.isSelected()) {
			type += IGraph.PSEUDO;
		}

		controller.newGraph(graphIF, type);

		graphIF.pack();
		graphIF.setVisible(true);

		createGraphDialog.setVisible(false);

}//GEN-LAST:event_createGraphDialog_createBActionPerformed

	private void createGraphDialog_nameTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_createGraphDialog_nameTextFieldKeyPressed

		if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
			this.createGraphDialog_createBActionPerformed(null);
		}

}//GEN-LAST:event_createGraphDialog_nameTextFieldKeyPressed

	private void file_openMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_file_openMIActionPerformed

		int returnval = openDialog.showOpenDialog(this);
		if (returnval == javax.swing.JFileChooser.APPROVE_OPTION) {
		}

	}//GEN-LAST:event_file_openMIActionPerformed

	private void createGraphDialog_cancelBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createGraphDialog_cancelBActionPerformed

		this.createGraphDialog_nameTextField.setText("");
		this.createGraphDialog.dispose();

}//GEN-LAST:event_createGraphDialog_cancelBActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				MainFrame mf = new MainFrame();
				mf.setController(Controller.getInstance());
				mf.setVisible(true);

			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog createGraphDialog;
    private javax.swing.JCheckBox createGraphDialog_MultipleCB;
    private javax.swing.JButton createGraphDialog_cancelB;
    private javax.swing.JButton createGraphDialog_createB;
    private javax.swing.JRadioButton createGraphDialog_digraphRB;
    private javax.swing.ButtonGroup createGraphDialog_graphKindsGroup;
    private javax.swing.JRadioButton createGraphDialog_graphRB;
    private javax.swing.JLabel createGraphDialog_nameLabel;
    private javax.swing.JTextField createGraphDialog_nameTextField;
    private javax.swing.JCheckBox createGraphDialog_pseudoCB;
    private javax.swing.JDesktopPane desktop;
    private javax.swing.JMenu editM;
    private javax.swing.JMenu fileM;
    private javax.swing.JMenuItem file_exitMI;
    private javax.swing.JMenuItem file_newGraphMI;
    private javax.swing.JMenuItem file_openMI;
    private javax.swing.JSeparator file_separator;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JFileChooser openDialog;
    // End of variables declaration//GEN-END:variables

	/** The controller instance of the MVC. */
	protected Controller controller;

	/**
	 * Returns the instance of the controller.
	 * @return The instance of the controller.
	 */
	public Controller getController() {
		return controller;
	}

	/**
	 * Sets the controller. Used to set the MVC.
	 * @param controller The instance of the controller.
	 */
	public void setController(Controller controller) {
		this.controller = controller;
	}
}
